<?php


namespace dsp\flyweight;


class Main extends \kernel\BaseMain
{

    protected function addCarToPoliceDatabase(
        FlyweightFactory $ff, $plates, $owner,
        $brand, $model, $color
    )
    {
        echo "\nClient: Adding a car to database.\n";
        $flyweight = $ff->getFlyweight([$brand, $model, $color]);

        // The client code either stores or calculates extrinsic state and passes it
        // to the flyweight's methods.
        $flyweight->operation([$plates, $owner]);
    }

    public function execute($choice = 'all')
    {
        $factory = new FlyweightFactory([
            ["Chevrolet", "Camaro2018", "pink"],
            ["Mercedes Benz", "C300", "black"],
            ["Mercedes Benz", "C500", "red"],
            ["BMW", "M5", "red"],
            ["BMW", "X6", "white"],
            // ...
        ]);
        $factory->listFlyweights();

        $this->addCarToPoliceDatabase($factory,
            "CL234IR",
            "James Doe",
            "BMW",
            "M5",
            "red");

        $this->addCarToPoliceDatabase($factory,
            "CL234IR",
            "James Doe",
            "BMW",
            "X1",
            "red");

        $factory->listFlyweights();
    }
}